﻿package 
{
	
	/**
	 * ...
	 * @author maximov
	 */
	import flash.geom.Vector3D;
	import jiglib.plugin.papervision3d.Papervision3DPhysics;
	import jiglib.physics.RigidBody;
	import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
    import org.papervision3d.materials.utils.MaterialsList;

	// Maze definition and creation... It's simple enough.
	public class Maze 
	{
		public static var factor:Number=8;
		public static function get startpos():Vector3D 
		{
			var v:Vector3D = new Vector3D(-5, -2, 1);
			v.scaleBy(factor);
			return v;
		}
		public static function get finishpos():Vector3D
		{
			var v:Vector3D = new Vector3D(5, 2, 1);
			v.scaleBy(factor);
			return v;
		}
		public static function CreateMaze(phys:Papervision3DPhysics, mat:Object):Array
		{
			var t:Number = 1;
			var walls:Array = [
						   {x:0,    y: 0,   z:0.5, w:16, h:10, d:t},
						   {x:0,    y: 4.5, z:  4, w:16, h:t,  d:6},
						   {x:0,    y:-4.5, z:  4, w:16, h:t,  d:6},
						   {x:-7.5, y:   0, z:  4, w:t , h:8,  d:6},
						   {x: 7.5, y:   0, z:  4, w:t , h:8,  d:6},
						   {x:-2.5, y:-2.5, z:  4, w:t , h:5,  d:6},
						   {x: 2.5, y: 2.5, z:  4, w:t , h:5,  d:6},
						  ];
						  
			for( var k:String in walls)
				for(var kk:String in walls[k])
				   walls[k][kk]*=factor;
				   
			var bodies:Array=new Array;
			for(var i:String in walls )
			{   
				var w:Object=walls[i];
				var pos:Vector3D=new Vector3D(w.x,w.y,w.z);
				var dims:Vector3D = new Vector3D(w.w, w.d, w.h);
				bodies.push(create_wall(phys, mat, pos, dims));
			}
			return bodies;
		}
		private static function create_wall(phys:Papervision3DPhysics,materials:Object,pos:Vector3D, dims:Vector3D):RigidBody
		{
		    var seg:Number = 6;
			
		    var b:RigidBody = phys.createCube(new MaterialsList(materials), dims.x, dims.y, dims.z, seg, seg, seg);
			phys.getMesh(b).culled = true;
			b.x=pos.x;
			b.y=pos.y;
			b.z=pos.z;
			b.mass=800;
			return b;
		}
		
	}
	
}